package com.aaa.sbms.dao;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * fileName:UserDao
 * description:
 * author:zz
 * createTime:2019/11/26 16:37
 * version:1.0.0
 */
public interface UserDao {

    /**
     * 根据用户名查询用户信息（登录使用）
     * @param userName
     * @return
     */
    @Select("select user_id, dept_id, login_name, user_name, user_type, email, phonenumber, sex, avatar, password, salt, status, del_flag, login_ip, login_date, create_by, create_time, update_by, update_time from sys_user where  del_flag=0 and user_name=#{userName}")
    Map  getUserByUserName(String userName);

    /**
     * 前台检测账号密码
     * @param map
     * @return
     */
    @Select("select * from users where userid=#{userid} and password=#{password} and state=0")
    Map isUsernameAndPassword(Map map);

    /**
     * 根据条件查询
     * @param map
     * @return
     */
    @Select("<script>select sys_user.user_id,dept_id,login_name,user_name,r.role_name,email,phonenumber,sex,avatar,password," +
            "salt,sys_user.status,sys_user.del_flag,login_ip,login_date,sys_user.create_by,sys_user.create_time," +
            "sys_user.update_by,sys_user.update_time " +
            "from sys_user " +
            "left join sys_user_role sr on sr.user_id=sys_user.user_id\n" +
            "left join sys_role r on r.role_id=sr.role_id where sys_user.del_flag=0 " +
            "<if test=\"loginName!=null and loginName!=''\"> and login_name like concat('%',#{loginName},'%') </if>" +
            "<if test=\"userName!=null and userName!=''\"> and user_name=#{userName} </if>" +
            "<if test=\"passWord!=null and passWord!=''\"> and password=#{passWord} </if>" +
            "<if test=\"email!=null and email!=''\"> and email like concat('%',#{email},'%') </if>" +
            "<if test=\"phoneNumber!=null and phoneNumber!=''\"> and phonenumber like concat('%',#{phoneNumber},'%')  </if>" +
            "<if test=\"status!=null and status!=''\">  and sys.user.status=#{status} </if>" +
            "limit #{begin},#{pageSize}" +
            "</script>")
    List<Map> list(Map map);

    /**
     * 总数量
     * @param map
     * @return
     */
    @Select("<script>select count(*) from sys_user " +
            " where del_flag=0 " +
            "<if test=\"loginName!=null and loginName!=''\"> and login_name like concat('%',#{loginName},'%') </if>" +
            "<if test=\"userName!=null and userName!=''\"> and user_name=#{userName} </if>" +
            "<if test=\"passWord!=null and passWord!=''\"> and password=#{passWord} </if>" +
            "<if test=\"email!=null and email!=''\"> and email like concat('%',#{email},'%') </if>" +
            "<if test=\"phoneNumber!=null and phoneNumber!=''\"> and phonenumber like concat('%',#{phoneNumber},'%')  </if>" +
            "<if test=\"status!=null and status!=''\">  and status=#{status} </if>" +
            "</script>")
    int listCount(Map map);

    /**
     * 根据id查询用户信息
     * @param userid
     * @return
     */
    @Select("select user_id userid,login_name username from sys_user where user_name=#{userid}")
    Map getUserInSysuserByUserid(String userid);

    /***
     * 添加
     * @param map
     * @return
     */
    @Insert("insert into sys_user (login_name, user_name, user_type, email, phonenumber, sex, avatar, password, salt, status, del_flag, login_ip, login_date, create_by, create_time, update_by, update_time) VALUES " +
            "(#{login_name},#{user_name},#{user_type},#{email},#{phonenumber},#{sex},#{avatar},#{password},#{salt}" +
            ",0,0,null,null,#{userName},now(),#{userName},now())")
    int add(Map map);

    /**
     * 更新
     * @param map
     * @return
     */
    @Update("update sys_user set login_name=#{login_name}, user_name=#{user_name}, user_type=#{user_type}," +
            " email=#{email}, phonenumber=#{phonenumber}, sex=#{sex}, avatar=#{avatar},password=#{password}, " +
            "salt=#{salt}, status=#{status},update_by=#{userName}, update_time=now() " +
            " where user_id=#{user_id}")
    int update(Map map);

    /**
     * 删除
     * @param userId
     * @return
     */
    @Update("update sys_user set del_flag=2 where user_id=#{userId}")
    int delete(int userId);

    /**
     * 查询用户id对应的角色id集合
     * @param userId
     * @return
     */
    @Select("select r.role_id,r.role_name from sys_user_role sr\n" +
            "left join sys_role r on r.role_id=sr.role_id where user_id=#{userId}")
    List<Integer>  listRoleIdsByUserId(int userId);

    /**
     * 删除原有用户ID关联的所有角色
     * @param userId
     * @return
     */
    @Delete("delete from sys_user_role where user_id=#{userId}")
    int delteRoleIdsByUserId(int userId);

    /**
     * 添加用户角色关联
     * @param values
     * @return
     */
    @Insert("insert into sys_user_role values ${values}")
    int addUserAndRole(String values);

    /**
     * 登录成功后，更新登录时间和登录IP
     * @param map
     * @return
     */
    @Update("update sys_user set login_ip=#{loginIp},login_date=now() where user_name=#{userName}")
    int updateLoginInfoByUserName(Map map);
}
